class Todo < ActiveRecord::Base
  belongs_to :user,
             :class_name => "User",
             :foreign_key => "user_id"

  validates_presence_of :title, :note
  validates_length_of :title, :in => 10..100, :message => "is too short or too long!"
  validates_length_of :note, :maximum => 1000
#  validates_attachment_size :briefcase, :less_than => 1.megabytes
  
  # define variable for uploading file
  has_attached_file :briefcase
  
  def validate
    unless (self.briefcase_file_size == nil) then
      if self.briefcase_file_size >=  Integer(1048576) then
        errors.add(:briefcase, "Briefcase file size file size must be less than 1048576 bytes.")
      end
    end
  end

  STATUS_TYPES = [
    [ "Done",  Integer(1) ],
    [ "Undone",   Integer(0) ]
  ]

  def  self.find_by_id (id)
    begin
      return self.find(id)
    rescue ActiveRecord::RecordNotFound
#      flash[:notice] = "Invalid todo"
      return nil
    end
  end

  def friendly_status
    done_num = Integer(1)
    undone_num = Integer(0)
    if self.status.eql?(done_num)
      return 'Done'
    elsif self.status.eql?(undone_num)
      return 'Not done'
    else
      return 'Undefined'
    end
  end

  def self.per_page
      2
  end
end
